package com.shaomengjie.smjmvp.utils;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.util.Log;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.RelativeLayout;

import com.shaomengjie.smjmvp.R;
import com.shaomengjie.smjmvp.framework.Trace;

/**
 * Created by shaomengjie on 2017/4/24.
 */

public class AnimUtils {
    public static void setRegisterUpfold(View view, int centerX,  int centerY, float startRadius, float endRadius){
        final float finalRadius = (float) Math.hypot(view.getWidth(), view.getHeight());
        Animator anim = ViewAnimationUtils.createCircularReveal(view, centerX, centerY, startRadius, finalRadius);
        anim.setDuration(500);
        anim.setInterpolator(new AccelerateDecelerateInterpolator());
        anim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
            }
        });
        anim.start();
    }
    public static void setButtonEnterPage(RelativeLayout temp){
        float curTranslationX = temp.getTranslationY();
        temp.setBackground(null);
        ObjectAnimator animator = ObjectAnimator.ofFloat(temp, "translationX", curTranslationX, -100f);
        ObjectAnimator animator1 = ObjectAnimator.ofFloat(temp, "rotation", 45f);
        animator1.setDuration(500);
        ObjectAnimator animator3 = ObjectAnimator.ofFloat(temp, "translationY", curTranslationX, 50f, 0f);
        animator3.setDuration(500);
        ObjectAnimator animator2 = ObjectAnimator.ofFloat(temp, "alpha", 0f, 1f);
        AnimatorSet set = new AnimatorSet();
        set.play(animator).with(animator1).with(animator2).with(animator3);
        animator.setDuration(500);
        set.start();
    }

    public static void setButtonOutPage(final RelativeLayout temp){
        float curTranslationX = temp.getTranslationY();
        ObjectAnimator animator = ObjectAnimator.ofFloat(temp, "translationX", 0);
        animator.setDuration(500);
        ObjectAnimator animator1 = ObjectAnimator.ofFloat(temp, "rotation", -90f);
        animator1.setDuration(500);
        ObjectAnimator animator3 = ObjectAnimator.ofFloat(temp, "translationY", curTranslationX, 50f, 0f);
        animator3.setDuration(500);
        ObjectAnimator animator2 = ObjectAnimator.ofFloat(temp, "alpha", 1f, 0f, 1f);
        animator2.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                Trace.d("valueAnimator>>>>>>",valueAnimator.getAnimatedValue().toString());
                float abs = Math.abs((Float) valueAnimator.getAnimatedValue());
                if (abs <= 0.3f) {
                    temp.setBackgroundResource(R.drawable.r_radiuo);
                }
            }
        });
        animator2.setDuration(500);
        AnimatorSet set = new AnimatorSet();
        set.play(animator1).with(animator3).with(animator).with(animator2);
        // animator.setStartDelay(delay);
        set.start();
    }
}
